package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.AccountInfoService;
import com.qigao.mall.commons.dto.AccountInfoQueryDto;
import com.qigao.mall.commons.dto.AccountInfoResultDto;
import com.qigao.mall.commons.dto.AccountLogCondDto;
import com.qigao.mall.commons.dto.AccountLogResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.vo.admin.merchant.AccountInfoQueryVo;
import com.qigao.mall.web.vo.admin.merchant.AccountLogCondVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app.merchant
 * @ProjectName three-mall
 * @date 2020-04-2020/4/13 9:58
 * @Description:账户操作相关
 */
@RestController
@RequestMapping("/admin/account/")
@Api(tags = "Admin账户余额相关")
public class AdminAccountController extends AbstractExcelController {
    private static Logger logger= LoggerFactory.getLogger(AdminAccountController.class);
    @Resource
    AccountInfoService accountInfoService;
    @PostMapping("/listPage")
    @ApiOperation(value = "账户余额分页查询列表", notes = "账户余额分页查询列表")
    public Result<PageInfo<AccountInfoResultDto>> listPage(@RequestBody AccountInfoQueryVo accountInfoQueryVo, HttpServletRequest request){
        try{
            //参数校验
            AccountInfoQueryDto accountInfoQueryDto=new AccountInfoQueryDto();
            BeanUtils.copyProperties(accountInfoQueryVo,accountInfoQueryDto);
            return  accountInfoService.listPage(accountInfoQueryDto);
        }catch (Exception e){
            logger.error("[账户余额分页查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(AccountInfoQueryVo accountInfoQueryVo, HttpServletResponse response) {
        AccountInfoQueryDto accountInfoQueryDto=new AccountInfoQueryDto();
        BeanUtils.copyProperties(accountInfoQueryVo,accountInfoQueryDto);
        // 待导出数据
        List<AccountInfoResultDto> orderResultDtos = accountInfoService.findOrderResultByCond(accountInfoQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "账户余额报表", excelUtils.getWorkbook());
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody AccountInfoQueryVo accountInfoQueryVo, HttpServletResponse response) {
        AccountInfoQueryDto accountInfoQueryDto=new AccountInfoQueryDto();
        BeanUtils.copyProperties(accountInfoQueryVo,accountInfoQueryDto);
        // 待导出数据
        List<AccountInfoResultDto> orderResultDtos = accountInfoService.findOrderResultByCond(accountInfoQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "账户余额报表", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "所属账户"),
                new ExcelHeaderInfo(0, 0, 1, 1, "账户余额"),
                new ExcelHeaderInfo(0, 0, 2, 2, "所属公司")
        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }
}
